import util
import codecs
def processDirectory(directory):
    
    for file in util.all_files(directory, '*.txt'):
        print 'Parsing file %s' % file
        convertEnc(file)

def convertEnc(filename, filename2=None, fromEnc='greek', toEnc='utf-8'):
    if not filename2: filename2 = filename+'.utf'
    file = codecs.open(filename, 'r', fromEnc, 'replace')
    contents = file.read()
    file2 = codecs.open(filename2, 'w', toEnc, 'replace')
    file2.write(contents)
    file2.flush()
    file2.close()

if __name__=='__main__':
    import sys
    print sys.argv
    root, out = None, None
    try:
        root = sys.argv[1]
        out = sys.argv[2]
    except:
        pass
    import os
    
    
    if(root):
        if os.path.isfile(root):
            if(out):
                convertEnc(root, out)
            else:
                convertEnc(root)               
        else:
            processDirectory(root)    